package com.vunguyen.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.vunguyen.model.Congvan;
import com.vunguyen.model.Congvanden;
import com.vunguyen.service.CongVanService;

@Controller
public class UploadController {
    @Autowired
    private CongVanService congvanService;
	
	
	@RequestMapping(value = "/download/{idcongvan}", method = RequestMethod.GET)
	  public String download(HttpServletResponse response,@PathVariable String idcongvan){
		
		Congvan congvan = congvanService.getCongvanById(idcongvan);
		try {

			OutputStream out = response.getOutputStream();
			response.setContentType(congvan.getFileType());
			response.setContentLengthLong(congvan.getFileLength());
			FileCopyUtils.copy(congvan.getFileBytes(), response.getOutputStream());
			out.flush();
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	  }
	
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	   public @ResponseBody String upload(MultipartHttpServletRequest request, HttpServletResponse response) throws ParseException {                 
	 
	     //0. notice, we have used MultipartHttpServletRequest
	 
	     //1. get the files from the request object
	     Iterator<String> itr =  request.getFileNames();
	 
	     MultipartFile mpf = request.getFile(itr.next());
	     System.out.println(mpf.getOriginalFilename() +" uploaded!");
	     Congvan cv = new Congvan();
	     Congvanden cvd = new Congvanden();
	 
	     try {
	                //just temporary save file info into ufile
				cv.setIdCongVan(request.getParameter("idcongvan"));
				cv.setTrichYeu(request.getParameter("trichyeu"));
				cv.setFileLength((long) mpf.getBytes().length);
				cv.setFileBytes(mpf.getBytes());
				cv.setFileType(mpf.getContentType());
				cv.setFileName(mpf.getOriginalFilename());
				congvanService.addCongVan(cv);
				cvd.setIdCongVanDen(request.getParameter("idcongvan"));
				cvd.setCongvan(cv);
				cvd.setNgayNhan(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("ngaynhan")));
				cvd.setNoiGui(request.getParameter("noigui"));
		    	congvanService.addCongVanDen(cvd);
	 
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }
	 
	     //2. send it back to the client as <img> that calls get method
	     //we are using getTimeInMillis to avoid server cached image 
	 
	     return "thuky";
	 
	  }

}
